


/**
 * primitive Cylinder 大量加载
 * 
 */
import Primitive from "../Primitive"

const DEFAULT_STYLE = {
  extrudedHeight : 0
}

export default class PolygonPrimitive extends Primitive {
  /**
   * 
   * @param {*} position 
   * @param {*} options 
   */
  constructor(positions, options = {}) {
    super()
    this._positions = positions
    this._type = 'polygon_primitive'
    this._style = {
      ...DEFAULT_STYLE,
      ...options
    }

    this.delegate = new Cesium.Primitive({
      geometryInstances: new Cesium.GeometryInstance({
        geometry: this.createGeometry()
      }),
      // releaseGeometryInstances: false
    })
    
    this.mounted()
  }

  /**
   * 初始化
   */
  mounted() {
    this.positions = this._positions
    this.style = this._style
    !this.delegate.appearance && this.setAppearance({ 
      type: 'Color',
      color : Cesium.Color.fromRandom({alpha : 0.7}) 
    })
  }
  /**
   * 创建图形
   * @returns 
   */
  createGeometry() {
    let polygon = new Cesium.PolygonGeometry({
      polygonHierarchy : new Cesium.PolygonHierarchy(this._positions),
      ...this.style
    })
    return Cesium.PolygonGeometry.createGeometry(polygon)
  }

  /**
   * 设置geometry样式
   * @param {*} style 
   * @returns 
   */
  setStyle(style) {
    if (!style) return
    Object.assign(this._style, { ...style })
    this.delegate.geometryInstances.geometry = this.createGeometry()
  }

  get positions() {
    return this._positions
  }

  set positions(val) {
    this._positions = val
    this.delegate.geometryInstances.geometry.polygonHierarchy = new Cesium.PolygonHierarchy(this._positions)
  }
}
